package com.ymx.darling.dialect;

import com.ymx.darling.bean.sql.BoundSql;
import com.ymx.darling.bean.sql.SqlStatement;

import java.util.List;

public class PostgresDialect implements Dialect{

    private String splitSql(String sql){
        return sql +" LIMIT ? OFFSET ?";
    }

    @Override
    public String getDataBaseType() {
        return "postgres";
    }

    @Override
    public void paginate(BoundSql boundSql, int pageNum, int pageSize) {
        List<Object> paramList = boundSql.getRightParameters();
        int startRow = (pageNum - 1) * pageSize;
        paramList.add(pageSize);
        paramList.add(startRow);

        boundSql.setSql(this.splitSql(boundSql.getSql()));
    }

    @Override
    public void limit(SqlStatement statement, long limit, long offset) {
        List<Object> paramList = statement.getParamList();
        paramList.add(limit);
        paramList.add(offset);
        statement.setSql(this.splitSql(statement.getSql()));
    }


    @Override
    public String subString(String column, int start, int length) {
        return String.format("%s FROM %s FOR %s", column, start, length);
    }
}
